package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbOrderItemMapper;
import com.dongdongshop.mapper.TbOrderMapper;
import com.dongdongshop.pojo.TbOrder;
import com.dongdongshop.pojo.TbOrderExample;
import com.dongdongshop.pojo.TbOrderItem;
import com.dongdongshop.pojo.TbOrderItemExample;
import com.dongdongshop.service.TbOrderItemService;
import com.dongdongshop.util.IdWorker;
import com.dongdongshop.vo.CartVo;
import com.dongdongshop.vo.TbAddressVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@DubboService
public class TbOrderItemServiceImpl implements TbOrderItemService {

    //注入mapper
    @Autowired
    private TbOrderItemMapper tbOrderItemMapper;
    @Autowired
    private TbOrderMapper tbOrderMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    //将订单从redis删除,平且存入数据库
    @Transactional
    @Override
    public String add(TbAddressVo vo, List<TbOrderItem> list, BigDecimal payment, String userid, Long id) {
        List<CartVo> lists = (List<CartVo>) redisTemplate.boundHashOps("cartList").get(id);
        IdWorker idWorker = new IdWorker();
        long l = idWorker.nextId();//唯一商品订单号
        String s = Long.toString(l);
        for (int i = 0; i < list.size(); i++) {
            for (int j = 0; j < lists.size(); j++) {
                for (int k = 0; k < lists.get(j).getCartDatailList().size(); k++) {
                    Long itemId = lists.get(j).getCartDatailList().get(k).getItemId();
                    Long itemId1 = list.get(i).getItemId();
                    if (itemId.equals(itemId1)){
                        lists.get(j).getCartDatailList().remove(k);
                    }
                }
            }
            long orderId = idWorker.nextId();
            long orderId1 = idWorker.nextId();
            list.get(i).setId(orderId);
            list.get(i).setOrderId(orderId1);
            list.get(i).setOrderNum(s);
            tbOrderItemMapper.insertSelective(list.get(i));
            TbOrder tbOrder = new TbOrder();
            tbOrder.setOrderId(orderId1);
            tbOrder.setPayment(payment);
            tbOrder.setPaymentType("1");
            tbOrder.setStatus("1");
            tbOrder.setCreateTime(new Date());
            tbOrder.setUpdateTime(new Date());
            tbOrder.setUserId(userid);
            tbOrder.setReceiverAreaName(vo.getProvince() + ' ' + vo.getCity() + ' ' + vo.getArea() + ' ' + vo.getAddress());
            tbOrder.setReceiverMobile(vo.getMobile());
            tbOrder.setReceiver(vo.getContact());
            tbOrder.setSellerId(list.get(i).getSellerId());
            tbOrder.setOrderNum(s);
            tbOrderMapper.insertSelective(tbOrder);
        }
        for (int i = 0; i < lists.size(); i++) {
            if (lists.get(i).getCartDatailList().size()==0){
                lists.remove(i);
            }
        }
        redisTemplate.delete("cartList");
        redisTemplate.boundHashOps("cartList").put(id,lists);
        return s;
    }

    //修改订单状态
    @Transactional
    @Override
    public void updatezhuangtai(String out_trade_no,String trade_no) {
        TbOrderExample te = new TbOrderExample();
        TbOrderExample.Criteria criteria = te.createCriteria();
        criteria.andOrderNumEqualTo(out_trade_no);
        List<TbOrder> list = tbOrderMapper.selectByExample(te);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setStatus("2");
            list.get(i).setOrderNo(trade_no);
            list.get(i).setPaymentTime(new Date());
            tbOrderMapper.updateByPrimaryKeySelective( list.get(i));
        }
    }

    //获取代发货数据
    @Override
    public List<TbOrder> showOrder() {
        TbOrderExample te = new TbOrderExample();
        TbOrderExample.Criteria criteria = te.createCriteria();
        criteria.andStatusEqualTo("2");
        return tbOrderMapper.selectByExample(te);
    }

    //退款成功
    @Override
    public void updatatuikuan(String out_biz_no) {
        TbOrderExample te = new TbOrderExample();
        TbOrderExample.Criteria criteria = te.createCriteria();
        Long aLong = Long.valueOf(out_biz_no);
        criteria.andOrderIdEqualTo(aLong);
        List<TbOrder> list = tbOrderMapper.selectByExample(te);
        list.get(0).setStatus("3");
        tbOrderMapper.updateByPrimaryKeySelective(list.get(0));
    }
}
